﻿using System;
using System.Text;
using System.Web;
using System.Web.Hosting;
using System.Web.Routing;
using CustomViewEngine.Controllers;
using WroxViewEngine;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Web.Mvc;
using System.IO;

namespace WroxViewEngine.Test
{
    
    
    /// <summary>
    ///This is a test class for WroxViewTest and is intended
    ///to contain all WroxViewTest Unit Tests
    ///</summary>
    [TestClass()]
    public class WroxViewTest
    {

        private const string virtualFile = "<html>\r\n<head>\r\n<title>w[Title]</title>\r\n</head>\r\n<body>\r\n<h2>w[Message]</h2>\r\n<p>\r\nTo learn more about ASP.NET MVC visit <a href=\"http://asp.net/mvc\" title=\"ASP.NET MVC Website\">http://asp.net/mvc</a>.\r\n</p>\r\n</body>\r\n</html>";

        /// <summary>
        ///A test for Render
        ///</summary>
        [TestMethod()]
        public void RenderTest()
        {

            WroxView view = new WroxView("~/Views/Home/Index.wrox");

            view.VPP = new FakeVPP(virtualFile);

            ViewDataDictionary ViewData = new ViewDataDictionary();
            ViewData["Title"] = "Home Page From Wrox View";
            ViewData["Message"] = "Welcome to ASP.NET MVC using the Wrox View Engine!";
            
            ViewContext context = CreateFakeViewContext(ViewData, view);

            StringWriter writer = new StringWriter();

            view.Render(context, writer);
            string rendered = writer.ToString();
            string expected = "<html>\r\n<head>\r\n<title>Home Page From Wrox View</title>\r\n</head>\r\n<body>\r\n<h2>Welcome to ASP.NET MVC using the Wrox View Engine!</h2>\r\n<p>\r\nTo learn more about ASP.NET MVC visit <a href=\"http://asp.net/mvc\" title=\"ASP.NET MVC Website\">http://asp.net/mvc</a>.\r\n</p>\r\n</body>\r\n</html>";

            Assert.AreEqual(expected, rendered);
        }

        private static ViewContext CreateFakeViewContext(ViewDataDictionary ViewData, WroxView view)
        {
            RouteData routeData = new RouteData();
            ControllerContext controllerContext = new ControllerContext(new FakeHttpContextBase(), routeData, new HomeController());
            return new ViewContext(controllerContext,view, ViewData, new TempDataDictionary());
        }

        internal class FakeHttpContextBase : HttpContextBase
        {

        }
    }

    internal class FakeVPP : VirtualPathProvider
    {
        private string _fakeFile;
        public FakeVPP(string fakeFile)
        {
            _fakeFile = fakeFile;
        }

        public override VirtualFile GetFile(string virtualPath)
        {
            return new FakeVirtualFile(virtualPath, _fakeFile);
        }
    }

    internal class FakeVirtualFile : VirtualFile
    {
        private string _fakeFile;
        public FakeVirtualFile(string file, string fakeFile)
            : base(file)
        {
            _fakeFile = fakeFile;
        }

        public override Stream Open()
        {
            return new MemoryStream(ASCIIEncoding.Default.GetBytes(_fakeFile));
        }
    }
}
